//---------------Sonic CD UFO Power Up Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias object.value0 : Object.ExplosionDir
private alias object.value1 : Object.ExplosionXVel
private alias object.value2 : Object.ExplosionYVel
private alias object.value3 : Object.Timer
private alias object.value4 : Object.YVelocity
private alias object.value5 : Object.ScreenDepth // Not used in this script, but set by the parent UFO Object upon spawning
private alias object.value6 : Object.ExplosionTimer
private alias object.value7 : Object.StartResults

// Player aliases
private alias object.state : SSSonic.State

private alias 9 : SSSONIC_CAMERAPAN

// Stage Finish Aliases
private alias 8 : STAGEFINISH_LOADTIMEATTACK
private alias 9 : STAGEFINISH_LOADCREDITS

// SFX aliases
private alias 22 : SFX_G_EXPLOSION

// Game Mode Aliases
private alias 1 : MODE_TIMEATTACK


event ObjectDraw
	// If the Object isn't falling at a rate of 4px downwards per frame yet, then update movements
	if Object.YVelocity < 0x40000

		// Draw the Power Up icon
		DrawSpriteScreenXY(object.propertyValue, object.ixpos, object.iypos)

		// Apply an eighth pixel's worth of gravity per frame
		Object.YVelocity += 0x2000

		// And then update the Power Up's position itself
		object.ypos += Object.YVelocity
	end if

	// Create explosions as needed
	Object.ExplosionTimer++
	if Object.ExplosionTimer == 6
		Object.ExplosionTimer = 0
		PlaySfx(SFXName[Explosion], false)

		// Randomise the explosion offset a tad bit
		Rand(temp0, 48)
		temp0 -= 24
		temp0 += screen.xoffset
		temp0 <<= 16
		temp0 += Object.ExplosionXVel

		Rand(temp1, 48)
		temp1 -= 24
		temp1 += screen.yoffset
		temp1 <<= 16
		temp1 += Object.ExplosionYVel

		CreateTempObject(TypeName[Smoke Puff], 0, temp0, temp1)

		object[TempObjectPos].drawOrder = 4
	end if

	// This value is randomised by the parent UFO object already, this Object doesn't set it itself
	if Object.ExplosionDir > 50
		// Moving to the bottom right corner of the screen

		Object.ExplosionXVel += 0x20000
	else
		// Moving to the bottom left corner of the screen

		Object.ExplosionXVel -= 0x20000
	end if

	// Make the explosions gravitate to the bottom corner of the screen, at a rate of 0.75 pixels per frame
	Object.ExplosionYVel += 0xC000

	Object.Timer++
	if Object.Timer == 160
		if Object.StartResults == true
			// If this object was the spawned by the last UFO, then end the stage
			// -> StartResults is set by the parent UFO when spawning this Power Up

			//if Options.GameMode < MODE_TIMEATTACK
				if stage.actNum < 8
					// If in a normal Special Stage, then hand the rest of the ending sequence to Sonic

					SSSonic.State[2] = SSSONIC_CAMERAPAN
				else
					// If in the hidden Robotnik Special Stage, then go ahead and load the credits

					object[30].type = TypeName[Stage Finish]
					object[30].state = STAGEFINISH_LOADCREDITS
					object[30].drawOrder = 7
				end if
			//else
				// If in time attack, then go ahead and fade out to the TA Menu scene

			//	object[30].type = TypeName[Stage Finish]
			//	object[30].state = STAGEFINISH_LOADTIMEATTACK
			//	object[30].drawOrder = 0
			//end if
		end if

		object.type = TypeName[Blank Object]
	end if

end event


event ObjectStartup

	LoadSpriteSheet("Special/Objects.gif")

	// 0 - Ring Icon
	SpriteFrame(-8, -8, 16, 16, 1, 67)

	// 1 - Speed Shoes Icon
	SpriteFrame(-8, -8, 16, 16, 18, 67)

	// 2 - Clock Icon
	SpriteFrame(-8, -8, 16, 16, 35, 67)
	
	// 3 - Yellow Chao Drive Icon
	SpriteFrame(-8, -8, 16, 16, 256, 58)

end event


// ========================
// Editor Subs
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 67)

	// Although used by the object, it shouldn't be set by the editor

end event
